package pluginInterfaces.setup;

import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import pluginInterfaces.IPluginAlgorithm;
import pluginInterfaces.IRecommender;
import pluginInterfaces.defaultImpl.CollaborativeFilteringAlgorithm;

import modelInterfaces.IEventType;
import dao.DAOException;
import dao.DAOFactory;

import utils.weka.core.Option;
import utils.weka.core.OptionHandler;
import utils.weka.core.SelectedTag;
import utils.weka.core.Tag;
import utils.weka.core.Utils;


public class RecommenderSetup implements IRecommender, OptionHandler {
	

	private String name = "";
	private IPluginAlgorithm algorithm;
	private int eventTypeRecommended;
	private static final Tag[] Tag_EventType = eventTypeRecommendedArray();
	

	public RecommenderSetup() {
		this.algorithm = new CollaborativeFilteringAlgorithm();
	}

	private static Tag[] eventTypeRecommendedArray() {
		Tag[] eventTypeRecommendedArray = null;
		
		try
		{
			List<IEventType> eventTypeList = DAOFactory.getDAOFactory().getEventTypeDAO().retrieveAll();
			eventTypeRecommendedArray = new Tag[eventTypeList.size()];
			
			for (int i=0; i<eventTypeList.size(); i++)
			{
				eventTypeRecommendedArray[i] = new Tag(i,eventTypeList.get(i).getName());
			}
			
		}
		catch (DAOException e)
		{
			javax.swing.JOptionPane.showMessageDialog(null, e.getCode() + " - " + e.getMessage(), "Error",javax.swing.JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
		
		return eventTypeRecommendedArray;
	}	
	
	

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * 
	 */
	public String getName(){
		return this.name;
	}
	
	public IPluginAlgorithm getAlgorithm() {
		return this.algorithm;
	}

	public void setAlgorithm(IPluginAlgorithm algorithm) {
		this.algorithm = algorithm;
	}
	
    public SelectedTag getEventTypeRecommended(){
    	return new SelectedTag(this.eventTypeRecommended, this.Tag_EventType);
    }
    
    public void setEventTypeRecommended(SelectedTag newType) {
        if (newType.getTags() == this.Tag_EventType) {
          this.eventTypeRecommended = newType.getSelectedTag().getID();
        }
    }	

	public Enumeration listOptions(){
		Vector newVector = new Vector();
		newVector.addElement(new Option("Name","N",0,"-N"));
		newVector.addElement(new Option("Algorithm","A",0,"-A"));
		return newVector.elements();
	}
	
    public String[] getOptions() {
        String [] options = new String [6];
        int itr = 0;
        
        options[itr++] = "-N";
        options[itr++] = "" + this.name;
        
        options[itr++] = "-A";
        options[itr++] = "" + this.algorithm.getClass().getName();
        
        options[itr++] = "-E";
        options[itr++] = "" + this.eventTypeRecommended;        
        

        while (itr < options.length) {
          options[itr++] = "";
        }
        return options;
    }
	
	public void setOptions(String[] options) throws Exception {
        String blendStr = Utils.getOption('N', options);
        this.name = blendStr;
		
        String tmpStr     = Utils.getOption('A', options);
        String [] tmpOptions = Utils.splitOptions(tmpStr);
        if (tmpOptions.length != 0) {
          tmpStr        = tmpOptions[0];
          tmpOptions[0] = "";
          this.setAlgorithm((IPluginAlgorithm) Utils.forName(IPluginAlgorithm.class,tmpStr, tmpOptions));
        }
        
        blendStr = Utils.getOption('E', options);
        if (blendStr.length() != 0){
        	setEventTypeRecommended(new SelectedTag(Integer.parseInt(blendStr), this.Tag_EventType));
        }
        else{
        	setEventTypeRecommended(new SelectedTag(1, this.Tag_EventType));
        }        
        
        
        Utils.checkForRemainingOptions(options);
    }

	
	@Override
	public String toString()
	{
		return this.name;
	}

}
